package menu

import (
	"context"
	"go-zero-project/services/user/rpc/internal/mapper"

	"go-zero-project/services/user/rpc/internal/svc"
	"go-zero-project/services/user/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type MenuSelectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMenuSelectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MenuSelectLogic {
	return &MenuSelectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MenuSelectLogic) MenuSelect(in *pb.EmptyObj) (*pb.MenuSelectResp, error) {
	logx.Infof("pd.Empty", in)
	// 1. select menu not button list
	notButtonList, err := mapper.NewMenuMapper().QueryNotButtonList(l.svcCtx.DBEngin)
	if err != nil {
		return nil, err
	}
	// 2. 递归阻装关系
	var menuIdArr []int64
	for _, v := range notButtonList {
		menuIdArr = append(menuIdArr, v.MenuId)
	}
	newNavListLogic := NewNavListLogic(l.ctx, l.svcCtx)
	allMenuList, err := newNavListLogic.getAllMenuList(menuIdArr)
	if err != nil {
		return nil, err
	}
	return &pb.MenuSelectResp{MenuArr: allMenuList}, nil
}
